<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\TestApi;

use Carbon\Carbon;
use App\Models\Eloquent;

/**
 * Class QtpTestValidatorRe
 *
 * @property int $id
 * @property string $test_report_uid
 * @property int $test_module_id
 * @property int $test_api_id
 * @property int $api_id
 * @property int $param_id
 * @property int $validator_id
 * @property int $param_res_id
 * @property string $rule
 * @property string $expect_value
 * @property string $true_value
 * @property int $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 *
 * @package App\Models
 */
class TestValidatorRes extends Eloquent
{
	protected $table = 'test_validator_res';

	protected $casts = [
		'test_module_id' => 'int',
		'test_api_id' => 'int',
		'api_id' => 'int',
		'param_id' => 'int',
		'validator_id' => 'int',
		'param_res_id' => 'int',
		'status' => 'int'
	];

	protected $fillable = [
		'test_report_uid',
		'test_module_id',
		'test_api_id',
		'api_id',
		'param_id',
		'validator_id',
		'param_res_id',
		'rule',
		'expect_value',
		'true_value',
		'status'
	];

    public static function getByParamResId($param_res_id)
    {
        return self::where('param_res_id', $param_res_id)->orderBy("id", "ASC")->get();
    }

    // 0未验证 1接口参数请求错误 2请求成功未取到验证字段 3请求成功取到验证字段验证不通过 4请求成功取到验证字段验证通过
    const STATUS_INIT = 0;
    const STATUS_API_ERR = 1;
    const STATUS_NOT_KEY = 2;
    const STATUS_NOT_EQUAL = 3;
    const STATUS_SUCCESS = 4;
    public static $status_maps = [
        self::STATUS_INIT => '未验证',
        self::STATUS_API_ERR => '请求错误',
        self::STATUS_NOT_KEY => '未取到验证字段',
        self::STATUS_NOT_EQUAL => '验证字段验证不通过',
        self::STATUS_SUCCESS => '验证通过',
    ];

    public function label()
    {
        $testReport = TestReport::getByUid($this->test_report_uid);
        $this->test_adminer = $testReport->test_adminer ?? '';
        $this->created_adminer = $testReport->created_adminer ?? '';
        $this->testModule = TestModule::find($this->test_module_id);
        $this->testAPi = TestApi::find($this->test_api_id);
        $this->testParam = TestParamRes::find($this->param_res_id);
        $this->status_label = self::$status_maps[$this->status] ?? '';
        $this->tested_at = $testReport->tested_at ?? '';
        return $this;
    }


    public static $fillables = [
        'test_report_uid' => '测试报告唯一UUID',
        'test_module_id' => '测试模块ID',
        'test_api_id' => '测试接口ID',
        'api_id' => '原始接口ID',
        'param_id' => '原始参数ID',
        'validator_id' => '原始验证器ID',
        'param_res_id' => '测试用例ID',
        'rule' => '取样验证规则',
        'expect_value' => '期望值',
        'true_value' => '实际得到值',
        'status' => '状态'
    ];
}
